﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Reflection;
using System.IO;

using mbine.msgc.model;
using mbine.msgc.tool;
using mbine.msgc.controls;
using System.Threading;

namespace mbine.msgc.controls
{
    /// <summary>
    /// Interaction logic for UCChat.xaml
    /// </summary>
    public partial class UCChat : UserControl, IObserveRoster
    {
        public CThread Thread { get; set; }
        public CRelayMessage SendData { get; set; }
        public string ChatData { get; set; }
        public bool IsSet { get; set; }

        public event EventHandler EventChatSend;

        public UCChat()
        {
            InitializeComponent();
            IsSet = false;
        }

        public void GetMessage(CRelayMessage _msg)
        {
            _msg.receivedate = DateTime.Now;
            AddMessage(_msg);   //화면에 메시지를 출력한다.
            Thread.Add(_msg);   //이 스레드에 메시지를 추가한다.
        }

        public void AddMessage(CRelayMessage _msg)
        {
            Dispatcher.Invoke(new ThreadStart(() =>
            {
                TextBlock tb = new TextBlock();
                tb.Background = new SolidColorBrush((_msg.sender == CCUser.Instance.Email) ? Colors.LightGray : Colors.White);
                DockPanel.SetDock(tb, Dock.Bottom);
                tb.Text = _msg.sender + " : " + _msg.receivedate.ToShortTimeString() + " : " + _msg.data;
                ChatLog.Children.Add(tb);
            }));
        }

        private void ChatBox_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                var textRange = new TextRange(ChatBox.Document.ContentStart, ChatBox.Document.ContentEnd);
                ChatData = textRange.Text.Replace("\r\n\r\n", "");
                ChatBox.Document.Blocks.Clear();

                CConnection.Instance.Send(CChatMessage.Chat("chat", CCUser.Instance.Email
                    , (from p in Thread.Members select p.Email).ToList()
                    , ChatData
                    , Thread.Key
                    , null)
                );

                SendData = new CRelayMessage
                {
                    data = ChatData
                    ,Members = Thread.Members
                    ,mode = "chat"
                    ,roomkey = Thread.Key
                    ,sender = CCUser.Instance.Email
                    ,sentdate = DateTime.Now
                };

                if (EventChatSend != null)
                    EventChatSend(this, null);
            }
        }

        //이 스레드의 메시지들을 출력한다.
        public void LoadMessages(CThread _thread)
        {
            Thread = _thread;
            foreach (CRelayMessage msg in Thread.Messages)
                AddMessage(msg);

            IsSet = true;
        }

        public void UpdateRoster()
        {
            
        }

        public void Clear()
        {
            ChatLog.Children.Clear();
            IsSet = false;
        }
    }
}
